package top.infopub.mgr.mkt.web;

import com.alibaba.fastjson.JSON;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.mkt.CommonCreditDetailsDto;
import top.infopub.enums.mkt.ProProcessDetailTypeEnum;
import top.infopub.enums.mkt.ProProcesssStatusEnum;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.service.OAFlowService;
import top.infopub.mgr.mkt.service.BusinessDetailsService;
import top.infopub.mgr.mkt.service.CreditDetailsDtoService;
import top.infopub.mgr.mkt.service.ProProcessManageService;
import top.infopub.model.mkt.BidReview;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;


/**
 * @Author Warking
 * @Date 2021/5/6 20:42
 * @Version 1.0  商务标
 */
@Controller
@RequestMapping("/business")
public class BusinessDetailsController {

    @Autowired
    private BusinessDetailsService businessDetailsService;
    @Autowired
    private CreditDetailsDtoService creditDetailsDtoService;
    @Autowired
    private ProProcessManageService proProcessManageService;
    @Autowired
    private OAFlowService oaFlowService;
    //跳转
    @RequestMapping("/toDealOrDetail")
    public String queryById(HttpServletRequest request, String proCode, String id) {
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        request.setAttribute("proCode", proCode);
        request.setAttribute("id", id);
        request.setAttribute("isAuditFlag", "1");
        request.setAttribute("auditTabFlag", "0");
        BidReview bidReview = creditDetailsDtoService.selectByPrimaryKey(id);
        if (bidReview != null) {
            if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bidReview.getProcessStatus())
                    || StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS9.getCode(), bidReview.getProcessStatus())) {
                request.setAttribute("isAuditFlag", "0");
            }
            if (StringUtils.isNotBlank(bidReview.getProcessId()) && bidReview.getProcessId().length() > 10) {
                request.setAttribute("processId", bidReview.getProcessId());
                String auditTabFlag = proProcessManageService.checkShowAuditTab(bidReview.getProcessId(), vo);
                if (StringUtils.equals(ProProcesssStatusEnum.PRO_STATUS1.getCode(), bidReview.getProcessStatus())
                        && StringUtils.equals("0", auditTabFlag)) {
                    request.setAttribute("isAuditFlag", "0");
                }
                request.setAttribute("auditTabFlag", auditTabFlag);
            }
        }
        return "mkt/lay-tenderReview-businessStandard-detail";
    }

    //商业评审详情页面
    @RequestMapping("/queryDetail")
    @ResponseBody
    public CommonCreditDetailsDto queryDetail(String proCode) {
        CommonCreditDetailsDto dto = businessDetailsService.queryDetail(proCode);
        return dto;
    }

    //商务评审 标 保存提交
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String businessDetailsDto, String identifying, String code, String id) {
        if (StringUtils.isEmpty(businessDetailsDto)) {
            return RspVo.getFailureResponse(0, "数据为空");
        }
        String object = StringEscapeUtils.unescapeJava(businessDetailsDto);
        CommonCreditDetailsDto dto = JSON.parseObject(object, CommonCreditDetailsDto.class);
        if ("0".equals(identifying)) {//0是保存
            int i = businessDetailsService.save(dto, code);
            if (i > 0) {
                return RspVo.getSuccessResponse(0, "保存成功");
            } else {
                return RspVo.getFailureResponse(0, "保存失败");
            }
        } else { //1是提交
            //判断标书策划的状态
            String flag = creditDetailsDtoService.isFlag(id);
            if ("1".equals(flag) || "9".equals(flag)) {
                return RspVo.getFailureResponse(0, "审批中或审批已完成，修改失败");
            }
            BidReview bidReview = creditDetailsDtoService.selectByPrimaryKey(id);
            if (StringUtils.isNotBlank(bidReview.getOaId())) {
                oaFlowService.finishOA(bidReview.getOaId());
            }
            //提交流程  先保存
            int i = businessDetailsService.save(dto, code);
            if (i > 0) {//保存成的情况 走流程
                //登录人员信息
                AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
                //发起流程
                String process = proProcessManageService.sendProcess(ProProcessDetailTypeEnum.PRO_NODE_TYPE9, object, id, code, vo);
                if (!StringUtils.isEmpty(process)) {
                    return RspVo.getSuccessResponse(0, "提交成功");
                }
                return RspVo.getFailureResponse(0, "提交失败");
            } else { //提交-保存失败
                return RspVo.getFailureResponse(0, "提交失败");
            }
        }

    }
    //审批
    @RequestMapping("/auditProcess")
    @ResponseBody
    public RspVo auditProcess(String id, String processId, String status, String auditMsg) {
        //立项审批
        proProcessManageService.auditProject(id, processId, status, auditMsg, null);
        return RspVo.getSuccessResponse();
    }

    // 商务标 删除接口
    @RequestMapping("/delete")
    @ResponseBody
    public RspVo delete(String sguid) {
        if (sguid == null || sguid == "") {
            return RspVo.getFailureResponse(0, "删除失败");
        }
        int i = businessDetailsService.deleteBusinessDetail(sguid);
        if (i > 0) {
            return RspVo.getSuccessResponse(0, "删除成功");
        } else {
            return RspVo.getFailureResponse(0, "删除失败");
        }
    }
}
